Uec3.PackageAddWindow = Ext.extend(Ext.app.Module,{
	id: 'packageAdd',
   /*
    * Initialize this module.
    * This function is called at startup (page load/refresh).
    */
   init : function(initParams){
   
   },
 
   getComponent:function(){
   	
   		var giftCondition_Store = getDictionaryStore(Uec3.App.Constants.homeUrl,{group:'package_giftCondition'});
   	    var giftCondition_combox = getDictionaryComboBox(giftCondition_Store,'local','giftCondition','赠送条件','giftCondition','','100%');
   		   		
   		var add_form =new Ext.FormPanel({
   		    border:false,
   		    frame:true,
   		    labelWidth:80,
   		    items:[
   		       {
   		          layout:'form',
   		          items:[{xtype:'textfield',fieldLabel:'套餐编码',name:'code',anchor:'50%'}]
   		       },{
   		         layout:'column',
   		         items:[
   		           {
   		             columnWidth:0.5,
   		             items:[
   		                {
   		                 layout:'form',
   		                 items:[   		                  
   		                   {xtype:'textfield',fieldLabel:'套餐名称',allowBlank:false,name:'name',anchor:'100%'}
   		                 ]
   		                }
   		             ]
   		           },{
   		             columnWidth:0.5,
   		             items:[
   		               {
   		                 layout:'form',
   		                 labelAlign:'right',
   		                 items:[
   		                   {xtype:'numberfield',fieldLabel:'价格',allowBlank:false,name:'price',anchor:'100%'}
   		                 ]
   		               }
   		             ]
   		           }
   		         ]
   		       },{
   		          layout:'form',
   		          items:[{xtype:'textfield',fieldLabel:'描述',name:'description',anchor:'100%'}]
   		       },{
   		          layout:'column',
   		          items:[
   		           {
   		             columnWidth:0.5,
   		             items:[
   		               {
   		                 layout:'form',
   		                 items:[{xtype:'datefield',fieldLabel:'有效开始时间',allowBlank:false,name:'gmtBegin',format:'Y-m-d',anchor:'100%'}]
   		               }
   		             ]
   		           }, {
   		             columnWidth:0.5,
   		             items:[
   		               {
   		                 layout:'form',
   		                 items:[{xtype:'datefield',fieldLabel:'有效结束时间',allowBlank:false,name:'gmtEnd',format:'Y-m-d',anchor:'100%'}]
   		               }
   		             ]
   		           }
   		          ]
   		       },{
   		       	  layout:'column',
   		       	  itemId:'isGift',
   		          items:[
   		           {
   		             columnWidth:0.5,
   		             items:[
   		                  {
   		                    layout:'hbox',
   		                    items:[
   		                      {
   		                        xtype:'label',
   		                        width:85
   		                      },{
   		                   		xtype: 'checkboxgroup',  		                  
                           		items: [{boxLabel:'是否是赠送的服务',name:'isGift',
                           		        listeners:{
                           		           'check':function(Checkbox,checked){
                           		           	  var giftCondition = add_form.getComponent("isGift").getComponent("giftCondition_combox").get(0);
                           		              if(checked==true){ 
                           		              	this.setRawValue(1);
                           		              	giftCondition.get(0).allowBlank = false;
                           		                giftCondition.show();
                           		              }else{
                           		              	this.inputValue = 0;
                           		              	giftCondition.get(0).el.dom.value="";
                           		              	giftCondition.get(0).allowBlank = true;
                           		              	giftCondition.hide();
                           		              }
                           		           }
                           		        }
                           		       }]  		                  
   		                  	  }
   		                    ]
   		                  }  		                  
   		              ]
   		           },{
   		             columnWidth:0.5,
   		             itemId:'giftCondition_combox',
   		             items:[
   		               {
   		                 layout:'form',
   		                 labelAlign:'right',
   		                 hidden:true,
   		                 items:[giftCondition_combox]   		                
   		               }
   		             ]
   		           }
   		          ]
   		       },{
   		          xtype:'fieldset',
   		          title : '包含服务',
   		          itemId:'service_checkGroup',
   		          autoHeight:true
   		          /*items:[
   		             {
   		               xtype: 'checkboxgroup',
   		               fieldLabel: '选择包含服务'
					   //columns: 3,
                       //items:[{boxLabel: '未认证公司', name: 'companyType'}]
   		             }
   		          ]  	*/	          
   		       },{
   		          xtype:'fieldset',
   		          title : '适用的公司类型',
   		          itemId:'companyType_checkGroup',
   		          autoHeight:true
   		          /*items:[
   		             {
   		               xtype: 'checkboxgroup',
   		               fieldLabel: '选择公司类型',
					   columns: 3,
                       items: [
                			{boxLabel: '未认证公司', name: 'companyType'},
                			{boxLabel: '汽修厂/4S店', name: 'companyType'},
                			{boxLabel: '经销商', name: 'companyType'},
               		    	{boxLabel: '大宗贸易商', name: 'companyType'},   
               		    	{boxLabel: '汽车配件厂', name: 'companyType'}, 
               		    	{boxLabel: '保险公司', name: 'companyType'} 
            	  		]
   		             }
   		          ]  	   */		         
   		       }
   		    ],
   		    buttons: [{
                  text: '确定',
                  handler: addPackage
   		       },{
   		          text:'清空',
   		          handler: function(){
   		          	 add_form.buttons[0].enable();
   		             add_form.getForm().reset();
   		          }
   		       },{
                  text: '取消',
                  handler: function(){
                     add_form.ownerCt.close();
                  }
            }],
            buttonAlign:'center'
   		    
   		});
   		
   		
   	    function init(){
   	       giftCondition_Store.load();  	      	       
   	    }   	    
   	    init();
   	    
   	   
   		function getServiceCheckGroup(){
   	        var service_checkGroup=[];     		 		
   		    var services_store = new Ext.data.JsonStore({
   		       url:Uec3.App.Constants.homeUrl+"/companyPackage/selectableService.do", 
   		       root:'root',
   		       fields:['id','name']  		      
   	        });
   	        services_store.load({
   	             callback:function(records,options,success){
   	                 Ext.each(records,function(record,index){
   	                 	  var item = {boxLabel:record.data.name,name:'serviceIds',inputValue:record.data.id}; 
   	                      service_checkGroup.push(item);
   	                 }); 
   	                 var checkGroup = new Ext.form.CheckboxGroup({
   	                     fieldLabel: '选择包含服务',
   	                     allowBlank : false,
   	                     width : 360,
					     columns: 3,
                         items:service_checkGroup
   	                 });
   	                 add_form.getComponent("service_checkGroup").add(checkGroup); 
   	                 add_form.doLayout();
   	             }
   	        }); 
   		}
   	                
   	    function getCompanyTypeCheckGroup(){
   	        var companyType_checkGroup=[];
   	        var companyType_store = new Ext.data.JsonStore({
   		       url:Uec3.App.Constants.homeUrl+"/dictionary/findByVarGroup.do?group=company_rtype",
	           root: "",
	           fields:[{name:'id',type:'int',mapping:'varIndex'},{name:'name',type:'string',mapping:'varName'}]	           
   		    });
   		    companyType_store.load({
   		        callback:function(records,options,success){
   		        	Ext.each(records,function(record,index){
   	                    if(record.data.name != '货如轮转网'){
   	                         companyType_checkGroup.push({boxLabel:record.data.name,name:'companyTypes',inputValue:record.data.id})
   	                 	} 	           
   	                });
   	                var checkGroup = new Ext.form.CheckboxGroup({
   	                     fieldLabel: '选择公司类型',
   	                     allowBlank :false,
   	                     width : 360,
					     columns: 3,
					     autoHeight:true,
                         items:companyType_checkGroup
   	                });
   	                add_form.getComponent("companyType_checkGroup").add(checkGroup); 
   	                add_form.doLayout();
   		        }
   		    });  	           
   	    } 
   	    
   	    getServiceCheckGroup();
   	    getCompanyTypeCheckGroup();
   	    
   	    function addPackage(){
            if(add_form.getForm().isValid()){
               Ext.MessageBox.confirm("提示","确定保存该套餐?",function(btnId){
                    if(btnId == 'yes'){
                       var myMask = new Ext.LoadMask(add_form.ownerCt.el,{msg : "正在保存,请稍后....." });
                        myMask.show(); 
                        var queryParam = Ext.encode(add_form.getForm().getValues());
                        Ext.Ajax.request({		            
				      		url:Uec3.App.Constants.homeUrl+"/companyPackage/addPackage.do",
				      		method :"post",					   
				      		params:{queryParam:queryParam},
				      		timeout:180000,
				      		callback :function(options,success,response){
                 
                      		},
                      		success:function(response,options){
                      	 		var resp = Ext.decode(response.responseText);
				         		Ext.Msg.alert("提示",resp.msg);
				         		myMask.hide();
				         		add_form.buttons[0].disable();
                      		},
                      		failure:function(response,options){
                         		Ext.Msg.alert("提示","发生异常,保存套餐失败!");
                         		myMask.hide();
                      		}
		      			}); 
                    }               	
               });    	
            }
        } 
   	    
   	    return add_form;
   },
   
   /**
	* Create this modules window here.
	*/
   createWindow : function(){
    	var desktop = this.app.getDesktop();
        var win = desktop.getWindow('packageAddWindow');
        if (!win) {
			win = desktop.createWindow({
				        id : "packageAddWindow",
						title : '增加套餐',
						width : 500,
						autoHeight : true,
						iconCls : 'acc-icon',
					    //minWidth:1000,
						maximizable : true,
						resizable:false,
						maximized:false,
						//autoScroll : true,
						constrainHeader : true,
						taskbuttonTooltip : "增加套餐",
						items : [this.getComponent()]
					});
		} 
    	win.show();         
   }
});